package com.awakensoftware.hunterserver.web.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.awakensoftware.communicationprotocol.contractobjects.errors.ErrorContractObject;
import com.awakensoftware.communicationprotocol.contractobjects.treasures.TreasureTemplateContractObject;
import com.awakensoftware.hunterserver.serverentities.TreasureTemplate;
import com.awakensoftware.hunterserver.services.TreasureTemplateService;

@RestController
@RequestMapping("/rest/treasureTemplates/**")
public class TreasureTemplateAPIController extends
		RestEntityControllerBase<TreasureTemplate, TreasureTemplateContractObject> {

	@Autowired
	private TreasureTemplateService treasureTemplateService;

	@RequestMapping(value = "/instance/{id}", method = RequestMethod.GET, produces = "application/json")
	public TreasureTemplateContractObject getTreasureTemplateById(@PathVariable("id") long id) {
		TreasureTemplate treasureTemplate = treasureTemplateService.getById(id);
		return super.getRestResponseFromServerEntity(treasureTemplate);
	}

	@RequestMapping(value = "/new/{name}", method = RequestMethod.PUT, produces = "application/json")
	public TreasureTemplateContractObject createTreasureTemplate(@PathVariable("name") String name) {
		TreasureTemplate treasureTemplate = treasureTemplateService.create(name);
		return super.getRestResponseFromServerEntity(treasureTemplate);
	}

	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST, produces = "application/json")
	public TreasureTemplateContractObject updateTreasureTemplate(@PathVariable("id") long id,
			@RequestBody TreasureTemplateContractObject contractObject) {
		TreasureTemplate treasureTemplate = super.getServerEntityfromRestRequest(contractObject);
		treasureTemplateService.update(treasureTemplate);
		treasureTemplate = treasureTemplateService.getById(id);
		return super.getRestResponseFromServerEntity(treasureTemplate);
	}
	
	
	@RequestMapping(value = "/search/{text}", method = RequestMethod.GET, produces = "application/json")
	public List<TreasureTemplateContractObject> searchTreasureTemplates(@PathVariable("text") String text) {
		List<TreasureTemplate> treasureTemplates = treasureTemplateService.search(text);
		return super.getRestResponseFromServerEntities(treasureTemplates);
	} 
	
	

	@Override
	@ExceptionHandler(Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public ErrorContractObject handleException(Exception ex, HttpServletRequest request) {
		return super.getErrorContractObject(ex);
	}

}
